﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Interactivity;

namespace Phone7ActionPack.Actions
{
    //
    // If you want your Action to target elements other than its parent, extend your class
    // from TargetedTriggerAction instead of from TriggerAction
    //
    public class CallMethodAction : TargetedTriggerAction<FrameworkElement>
    {



        public string MethodArgumentPath
        {
            get { return (string)GetValue(MethodArgumentPathProperty); }
            set { SetValue(MethodArgumentPathProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MethodArgumentPath.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MethodArgumentPathProperty =
            DependencyProperty.Register("MethodArgumentPath", typeof(string), typeof(CallMethodAction), null);

        public string MethodName
        {
            get { return (string)GetValue(MethodNameProperty); }
            set { SetValue(MethodNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MethodName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MethodNameProperty =
            DependencyProperty.Register("MethodName", typeof(string), typeof(CallMethodAction), null);

        
        public CallMethodAction()
        {
            // Insert code required on object creation below this point.
        }

        protected override void Invoke(object o)
        {
            object[] arguments = new object[1];
            if(MethodArgumentPath != null)
                arguments[0]= this.AssociatedObject.GetType().GetProperty(MethodArgumentPath).GetValue(this.AssociatedObject,null);
            else 
            arguments = null;
            this.Target.GetType().GetMethod(this.MethodName).Invoke(this.Target,arguments);
        }
    }
}